enum InputType {
    ANY,
    NUMBER,
}
var _InputType = cc.Enum(InputType);

const { ccclass, property } = cc._decorator;

@ccclass
export default class NEditBox extends cc.Component {

    //挂在有editBox组件的节点上
    @property({
        type: _InputType
    })
    inputType = _InputType.ANY;

    @property(cc.Boolean)
    isSingleLine: boolean = true;
    // LIFE-CYCLE CALLBACKS:
    private editBox: cc.EditBox = null;
    private textLabel: cc.Label = null;
    private oldLength: number = 0;
    private maxLength: number = 0;

    onLoad() {
        this.init();
    }

    start() {

    }

    // update (dt) {}
    private init() {
        this.editBox = this.node.getComponent(cc.EditBox);
        if (!this.editBox) return false;
        this.editBox.inputMode = this.isSingleLine ? cc.EditBox.InputMode.SINGLE_LINE : cc.EditBox.InputMode.ANY;
        // let textNode = this.editBox.textLabel.node;

        // let newTextNode = cc.instantiate(textNode);
        // newTextNode.parent = this.node;
        // newTextNode.active = true;
        // newTextNode.setSiblingIndex(textNode.getSiblingIndex() + 1);
        // this.textLabel = newTextNode.getComponent(cc.Label);

        let beginHandler = new cc.Component.EventHandler();
        beginHandler.component = 'NEditBox';
        beginHandler.target = this.node;
        beginHandler.handler = 'editBegin';
        this.editBox.editingDidBegan.push(beginHandler);
        let changedHandler = new cc.Component.EventHandler();
        changedHandler.component = 'NEditBox';
        changedHandler.target = this.node;
        changedHandler.handler = 'textChanged';
        this.editBox.textChanged.push(changedHandler);
        let endedHandler = new cc.Component.EventHandler();
        endedHandler.component = 'NEditBox';
        endedHandler.target = this.node;
        endedHandler.handler = 'editEnded';
        this.editBox.editingDidEnded.push(endedHandler);
        let returnHandler = new cc.Component.EventHandler();
        returnHandler.component = 'NEditBox';
        returnHandler.target = this.node;
        returnHandler.handler = 'editReturn';
        this.editBox.editingReturn.push(returnHandler);
        return true;
    }
    editBegin() {
    }
    textChanged() {
        // let str = this.editBox.string;
    }
    editEnded() {
        // let str = this.textLabel.string;
    }
    editReturn() {
        this.editEnded();
    }
}
